package vip.xiaonuo.smzq.modular.psba.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.smzq.modular.deom.TypeProcessing;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb02Kslx;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb03Kspj;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb06Kc;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb13Zycllx;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb02KslxMapper;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb03KspjMapper;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb06KcMapper;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb13ZycllxMapper;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaGtKjzycl;
import vip.xiaonuo.smzq.modular.psba.entity.vo.CdfwZycledit;
import vip.xiaonuo.smzq.modular.psba.entity.vo.Cdfwnzycl;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaZycl;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaGtKjzyclMapper;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaGtZlMapper;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaGtKjzyclService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 评审备案—重叠范围内资源储量 服务实现类
 *
 * @author sqq
 * @since 2024-05-22
 */
@Service
public class PsbaGtKjzyclServiceImpl extends ServiceImpl<PsbaGtKjzyclMapper, PsbaGtKjzycl> implements IPsbaGtKjzyclService {

    @Resource
    private PsbaGtKjzyclMapper psbaGtKjzyclMapper;
    @Resource
    private Cb06KcMapper cb06KcMapper; //矿产
    @Resource
    private Cb03KspjMapper cb03KspjMapper;//矿石品级
    @Resource
    private Cb02KslxMapper cb02KslxMapper;//矿石类型
    @Resource
    private PsbaGtZlMapper psbaGtZlMapper;//矿石质量
    @Resource
    private Cb13ZycllxMapper cb13ZycllxMapper;


    @Override
    public Page<PsbaGtKjzycl> page(PsbaGtKjzycl psbaGtKjzycl) {
        QueryWrapper<PsbaGtKjzycl> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public List<Cdfwnzycl> getCdfwnzyclList(String psbabh) {
        List<Cdfwnzycl> cdfwnzyclList = new ArrayList<>();
//      try{
        List<String> kslxpjList = psbaGtKjzyclMapper.getKslxpjList(psbabh);
        if (kslxpjList != null && kslxpjList.size() > 0) {
            for (String kslxpj : kslxpjList) {
                // 使用逗号作为分隔符进行分割
                String[] parts = kslxpj.split(",");
                // 获取两个部分
                Integer kslx = Integer.valueOf(parts[0]);  // 逗号前的部分
                Integer kspj = Integer.valueOf(parts[1]);  // 逗号后（如果有的话）的部分
                List<String> zycllzList = psbaGtKjzyclMapper.getZycllzList(psbabh, kslx, kspj);
                if (zycllzList != null && zycllzList.size() > 0) {
                    for (String zycllz : zycllzList) {
                        QueryWrapper<PsbaGtKjzycl> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("psbabh", psbabh);
                        queryWrapper.eq("kslx", kslx);
                        queryWrapper.eq("kspj", kspj);
                        queryWrapper.eq("zycllz", zycllz);
                        List<PsbaGtKjzycl> psbaGtKjzyclList = psbaGtKjzyclMapper.selectList(queryWrapper);

                        if (psbaGtKjzyclList != null && psbaGtKjzyclList.size() > 0) {
                            PsbaGtKjzycl psbaGtKjzycl = psbaGtKjzyclList.get(0);
                            Cdfwnzycl cdfwnzycl = new Cdfwnzycl();
                            cdfwnzycl.setKqbh(psbaGtKjzycl.getKqbh());
                            cdfwnzycl.setSsksbh(psbaGtKjzycl.getSsksbh());
                            cdfwnzycl.setKczycllx(psbaGtKjzycl.getZycllz());

                            //矿石工业类型及品牌
                            String kslxpjmc = "";
                            QueryWrapper<Cb02Kslx> queryWrapperKslx = new QueryWrapper<>();
                            queryWrapperKslx.eq("kcdm", psbaGtKjzycl.getKcdm());
                            queryWrapperKslx.eq("kslx", psbaGtKjzycl.getKslx());
                            Cb02Kslx cb02Kslx = cb02KslxMapper.selectOne(queryWrapperKslx);
                            if (cb02Kslx != null) {
                                kslxpjmc = cb02Kslx.getKslxmc() + " ";
                            }

                            QueryWrapper<Cb03Kspj> queryWrapperKspj = new QueryWrapper<>();
                            queryWrapperKspj.eq("kcdm", psbaGtKjzycl.getKcdm());
                            queryWrapperKspj.eq("kspj", psbaGtKjzycl.getKspj());
                            Cb03Kspj cb03Kspj = cb03KspjMapper.selectOne(queryWrapperKspj);
                            if (cb03Kspj != null) {
                                kslxpjmc += cb03Kspj.getKspjmc();
                            }
                            cdfwnzycl.setKslxpj(kslxpjmc);

                            //矿产名称
                            String kcmc = "";
                            //统计对象
                            String tjdx = "";
                            QueryWrapper<Cb06Kc> queryWrapperKc = new QueryWrapper<>();
                            queryWrapperKc.eq("kcdm", psbaGtKjzycl.getKcdm());
                            List<Cb06Kc> cb06KcList = cb06KcMapper.selectList(queryWrapperKc);
                            if (cb06KcList != null && cb06KcList.size() != 0) {
                                Cb06Kc cb06Kc = cb06KcList.get(0);
                                kcmc = cb06Kc.getKcmc() + "(" + psbaGtKjzycl.getKczh() + ")";
                                tjdx = cb06Kc.getKcmc();
                                if (cb06Kc.getJsldw() != null && !cb06Kc.getJsldw().equals("")) {
                                    tjdx = tjdx + " " + cb06Kc.getJsldw();
                                } else {
                                    tjdx = tjdx + " " + cb06Kc.getKsldw();
                                }
                                cdfwnzycl.setKcmc(kcmc);
                                cdfwnzycl.setTjdxdw(tjdx);
                                for (PsbaGtKjzycl psbaGtKjzycl1 : psbaGtKjzyclList) {
                                    if (psbaGtKjzycl1.getZycllb() == 19001) {
                                        cdfwnzycl.setJslbul(psbaGtKjzycl1.getJsl());
                                        cdfwnzycl.setKslbul(psbaGtKjzycl1.getKsl());

                                    }
                                    if (psbaGtKjzycl1.getZycllb() == 19002) {
                                        cdfwnzycl.setKslcml(psbaGtKjzycl1.getKsl());
                                        cdfwnzycl.setJslcml(psbaGtKjzycl1.getJsl());

                                    }
                                }
                                cdfwnzyclList.add(cdfwnzycl);
                            }
                        }
                    }
                }
            }
        }
//        }catch (Exception e){
//
//        }
        return cdfwnzyclList;
    }

    /**
     * 重叠范围内资源储量查询
     *
     * @param psbabh
     * @return
     */
    @Override
    public List<CdfwZycledit> getCdfwZycleditList(String psbabh) {
        List<CdfwZycledit> cdfwZycleditList = new ArrayList<>();

        List<String> kslxpjList = psbaGtKjzyclMapper.getKslxpjList(psbabh);
        if (kslxpjList != null && kslxpjList.size() > 0) {
            for (String kslxpj : kslxpjList) {
                // 使用逗号作为分隔符进行分割
                String[] parts = kslxpj.split(",");
                // 获取两个部分
                Integer kslx = Integer.valueOf(parts[0]);  // 逗号前的部分
                Integer kspj = Integer.valueOf(parts[1]);  // 逗号后（如果有的话）的部分
                QueryWrapper<PsbaGtKjzycl> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("psbabh", psbabh);
                queryWrapper.eq("kslx", kslx);
                queryWrapper.eq("kspj", kspj);
                List<PsbaGtKjzycl> psbaGtKjzyclList = psbaGtKjzyclMapper.selectList(queryWrapper);
                List<String> zycllzList = psbaGtKjzyclMapper.getZycllzList(psbabh, kslx, kspj);
                if (psbaGtKjzyclList != null && psbaGtKjzyclList.size() > 0) {
                    PsbaGtKjzycl psbaGtKjzycl = psbaGtKjzyclList.get(0);
                    CdfwZycledit cdfwZycledit = new CdfwZycledit();
                    cdfwZycledit.setKqbh(psbaGtKjzycl.getKqbh());
                    cdfwZycledit.setSsksbh(psbaGtKjzycl.getSsksbh());
                    cdfwZycledit.setPsbabh(psbaGtKjzycl.getPsbabh());
                    cdfwZycledit.setKczh(psbaGtKjzycl.getKczh());
                    //矿石工业类型及品牌
                    String kslxpjmc = "";
                    QueryWrapper<Cb02Kslx> queryWrapperKslx = new QueryWrapper<>();
                    queryWrapperKslx.eq("kcdm", psbaGtKjzycl.getKcdm());
                    queryWrapperKslx.eq("kslx", psbaGtKjzycl.getKslx());
                    Cb02Kslx cb02Kslx = cb02KslxMapper.selectOne(queryWrapperKslx);
                    if (cb02Kslx != null) {
                        cdfwZycledit.setKslx(cb02Kslx.getKslxmc());
                        cdfwZycledit.setKslxDm(cb02Kslx.getKslx());
                    }

                    QueryWrapper<Cb03Kspj> queryWrapperKspj = new QueryWrapper<>();
                    queryWrapperKspj.eq("kcdm", psbaGtKjzycl.getKcdm());
                    queryWrapperKspj.eq("kspj", psbaGtKjzycl.getKspj());
                    Cb03Kspj cb03Kspj = cb03KspjMapper.selectOne(queryWrapperKspj);
                    if (cb03Kspj != null) {
                        cdfwZycledit.setKspj(cb03Kspj.getKspjmc());
                        cdfwZycledit.setKspjDm(cb03Kspj.getKspj());
                    }
                    //矿产名称
                    String kcmc = "";
                    //统计对象
                    String tjdx = "";
                    QueryWrapper<Cb06Kc> queryWrapperKc = new QueryWrapper<>();
                    queryWrapperKc.eq("kcdm", psbaGtKjzycl.getKcdm());
                    List<Cb06Kc> cb06KcList = cb06KcMapper.selectList(queryWrapperKc);
                    if (cb06KcList != null && cb06KcList.size() != 0) {
                        Cb06Kc cb06Kc = cb06KcList.get(0);
                        kcmc = cb06Kc.getKcmc();
                        if (cb06Kc.getJsldw() != null && !cb06Kc.getJsldw().equals("")) {
                            tjdx = kcmc + " " + cb06Kc.getJsldw();
                        } else {
                            tjdx = kcmc + " " + cb06Kc.getKsldw();
                        }
                        cdfwZycledit.setTjdx(tjdx);
                        cdfwZycledit.setKcmc(kcmc);

                        if (cb06Kc.getJsldw() != null && !cb06Kc.getJsldw().equals("")){
                            cdfwZycledit.setKcbm("3_"+cb06Kc.getKcdm()+"_"+cb06Kc.getTjdx());
                        }else{
                            cdfwZycledit.setKcbm("2_"+cb06Kc.getKcdm()+"_"+cb06Kc.getTjdx());
                        }
                        List<PsbaZycl> psbaZycls = new ArrayList<>();


                        for (String lxmc : zycllzList) {
                            QueryWrapper<Cb13Zycllx> queryWrapperZycllx = new QueryWrapper<>();
                            queryWrapperZycllx.eq("dm", lxmc);
                            Cb13Zycllx cb13Zycllx=cb13ZycllxMapper.selectOne(queryWrapperZycllx);
                            PsbaZycl psbaZycl = new PsbaZycl();
                            //ToDo 涉及新老数据库，需要修改同步
                            if (cb13Zycllx != null){
                                psbaZycl.setLxmc(cb13Zycllx.getMc());
                                psbaZycl.setLxbm(lxmc);
                            }else{
                                psbaZycl.setLxbm(lxmc);
                                psbaZycl.setLxmc(lxmc);
                            }
                              double nmbyJsl= 0.0;
                              double ljcmJsl= 0.0;
                              double nmbyKsl= 0.0;
                              double ljcmKsl= 0.0;

                            for (PsbaGtKjzycl psbaGtKjzycl2 : psbaGtKjzyclList) {
                                if (psbaGtKjzycl2.getZycllb() == 19001 && lxmc.equals(psbaGtKjzycl2.getZycllz())) {
                                    if (psbaGtKjzycl2.getJsl()!=null){
                                        nmbyJsl= psbaGtKjzycl2.getJsl();
                                    }
                                    nmbyKsl= psbaGtKjzycl2.getKsl();
                                }
                                if (psbaGtKjzycl2.getZycllb() == 19002 && lxmc.equals(psbaGtKjzycl2.getZycllz())) {
                                    if (psbaGtKjzycl2.getJsl()!=null){
                                        ljcmJsl= psbaGtKjzycl2.getJsl();
                                    }
                                    ljcmKsl= psbaGtKjzycl2.getKsl();

                                }
                            }
                            psbaZycl.setLjcmJsl(ljcmJsl);
                            psbaZycl.setLjcmKsl(ljcmKsl);
                            psbaZycl.setNmbyJsl(nmbyJsl);
                            psbaZycl.setNmbyKsl(nmbyKsl);
                            psbaZycls.add(psbaZycl);
                        }
                        cdfwZycledit.setReserveDatas(psbaZycls);
                        cdfwZycleditList.add(cdfwZycledit);
                    }
                }

            }
        }


        return cdfwZycleditList;
    }

    @Override
    public void addCdfwZycledits(List<CdfwZycledit> cdfwZycledits) {
        if (cdfwZycledits != null && cdfwZycledits.size() > 0) {

            //删除旧数据避免避免重复添加
            CdfwZycledit cdfwZyclget0 = cdfwZycledits.get(0);
            QueryWrapper<PsbaGtKjzycl> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("psbabh", cdfwZyclget0.getPsbabh());
            psbaGtKjzyclMapper.delete(queryWrapper);

            //开始处理添加数据
            for (CdfwZycledit cdfwZycledit : cdfwZycledits) {
                List<PsbaZycl> psbaZycls = cdfwZycledit.getReserveDatas();
                if (psbaZycls != null && psbaZycls.size() > 0) {
                    List<Integer> kzbhList = TypeProcessing.strToList(cdfwZycledit.getKcbm());
                    Integer kcdm = 0;
                    Integer tjdx=0;
                    if (kzbhList != null && kzbhList.size() ==3){
                        kcdm = kzbhList.get(1);
                        tjdx = kzbhList.get(2);
                    }



                    PsbaGtKjzycl psbaGtKjzycl = new PsbaGtKjzycl();
                    psbaGtKjzycl.setPsbabh(cdfwZycledit.getPsbabh());
                    psbaGtKjzycl.setKqbh(cdfwZycledit.getKqbh());
                    psbaGtKjzycl.setSsksbh(cdfwZycledit.getSsksbh());
                    psbaGtKjzycl.setKcdm(kcdm);
                    psbaGtKjzycl.setTjdx(tjdx);
                    psbaGtKjzycl.setKslx(cdfwZycledit.getKslxDm());
                    psbaGtKjzycl.setKspj(cdfwZycledit.getKspjDm());
                    psbaGtKjzycl.setKczh(cdfwZycledit.getKczh());
                    QueryWrapper<Cb06Kc> queryWrapperKc = new QueryWrapper<>();
                    queryWrapperKc.eq("kcdm", kcdm);
                    List<Cb06Kc> cb06KcList = cb06KcMapper.selectList(queryWrapperKc);

                    psbaGtKjzycl.setTjdx(cb06KcList.get(0).getTjdx());

                    for (PsbaZycl psbaZycl : psbaZycls) {
                        psbaGtKjzycl.setZycllz(psbaZycl.getLxbm());
                        String by = UUID.randomUUID().toString();
                        psbaGtKjzycl.setZycllb(19001);
                        psbaGtKjzycl.setJsl(psbaZycl.getNmbyJsl());
                        psbaGtKjzycl.setKsl(psbaZycl.getNmbyKsl());
                        psbaGtKjzycl.setId(by);
                        psbaGtKjzyclMapper.insert(psbaGtKjzycl);
                        psbaGtKjzycl.setZycllb(19002);
                        psbaGtKjzycl.setJsl(psbaZycl.getLjcmJsl());
                        psbaGtKjzycl.setKsl(psbaZycl.getLjcmKsl());
                        String lj = UUID.randomUUID().toString();
                        psbaGtKjzycl.setId(lj);
                        psbaGtKjzyclMapper.insert(psbaGtKjzycl);
                    }
                }
            }
        }
    }







}
